package com.github.caijh.mall.utils;

import java.util.Arrays;
import java.util.Optional;

import com.github.caijh.framework.core.exception.BizException;
import com.github.caijh.mall.upload.vo.FileResultVo;
import org.springframework.web.multipart.MultipartFile;

public interface UploadStrategy {

    FileResultVo upload(MultipartFile multipartFile, UploadSpecs fileSpecs);

    enum Type {
        LOCAL(1, "local"),
        QN(2, "qn"),
        ALI(3, "al"),
        TX(4, "tx");

        private final int index;
        private final String prefix;

        Type(int index, String prefix) {
            this.index = index;
            this.prefix = prefix;
        }

        public static Type get(int index) {
            Optional<Type> type = Arrays.stream(Type.values()).filter(e -> e.getIndex() == index).findFirst();
            if (!type.isPresent()) {
                throw BizException.of("UPLOAD_STRATEGY_NOT_FOUND");
            }
            return type.get();
        }

        public int getIndex() {
            return index;
        }

        public String getPrefix() {
            return prefix;
        }
    }

}
